package net.Stratus.ImageDumper.GUI;

import java.io.File;

import net.Stratus.ImageDumper.Job;
import net.Stratus.ImageDumper.Sites.Chan4;
import net.Stratus.ImageDumper.Sites.Flickr;

import org.eclipse.swt.widgets.DirectoryDialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;

public class AddJob {

	protected Shell shell;
	private Text txtUrl;
	private Text txtLimit;
	private Text txtDest;
	private Combo combo;
	
	/**
	 * Creates a new AddJob Window
	 * @wbp.parser.entryPoint
	 */
	public static void create() {
		try {
			AddJob window = new AddJob();
			window.open();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Open the window.
	 */
	public void open() {
		Display display = Display.getDefault();
		createContents();
		shell.open();
		shell.layout();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
	}

	/**
	 * Create contents of the window.
	 */
	protected void createContents() {
		shell = new Shell(SWT.CLOSE | SWT.TITLE | SWT.MIN | SWT.MAX);
		shell.setSize(275, 175);
		shell.setText("Image Dumper - Add Job");
		
		Label lblUrl = new Label(shell, SWT.NONE);
		lblUrl.setBounds(10, 10, 24, 15);
		lblUrl.setText("URL:");
		
		txtUrl = new Text(shell, SWT.BORDER);
		txtUrl.setBounds(40, 7, 219, 21);
		
		Label lblSite = new Label(shell, SWT.NONE);
		lblSite.setBounds(10, 34, 24, 15);
		lblSite.setText("Site:");
		
		combo = new Combo(shell, SWT.NONE);
		combo.setItems(new String[] {"flickr.com set", "4chan.org thread"});
		combo.setBounds(40, 31, 219, 23);
		combo.select(0);
		
		Label lblLimit = new Label(shell, SWT.NONE);
		lblLimit.setBounds(10, 60, 113, 15);
		lblLimit.setText("Limit (0 = Unlimited):");
		
		txtLimit = new Text(shell, SWT.BORDER);
		txtLimit.setText("0");
		txtLimit.setBounds(126, 57, 133, 21);
		
		Label lblDestFolder = new Label(shell, SWT.NONE);
		lblDestFolder.setBounds(10, 89, 30, 15);
		lblDestFolder.setText("Dest:");
		
		txtDest = new Text(shell, SWT.BORDER);
		txtDest.setBounds(40, 86, 141, 21);
		
		Button btnBrowse = new Button(shell, SWT.NONE);
		btnBrowse.setBounds(187, 84, 72, 25);
		btnBrowse.setText("Browse");
		btnBrowse.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				DirectoryDialog dd = new DirectoryDialog(shell);
				String dir = dd.open()+"\\";
				txtDest.setText((dir == null) ? "" : dir);
			}
		});
		
		Button btnAdd = new Button(shell, SWT.NONE);
		btnAdd.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				if(validateInput()) {
					final Job job = new Job(txtUrl.getText(), txtDest.getText());
					final int selection = combo.getSelectionIndex();
					
					// start thread
					Thread t = new Thread() {
						@Override
						public void run() {
							super.run();
							// create site object
							switch(selection) {
								case 0:
									Flickr f = new Flickr(job);
									f.dump();
									break;
								case 1:
									Chan4 c4 = new Chan4(job);
									c4.dump();
									break;
							}
						}			
					};
					t.start();
					shell.close();
				}		
			}
		});
		btnAdd.setBounds(10, 113, 249, 25);
		btnAdd.setText("Add New Job");

	}

	private boolean validateInput() {
		if(txtUrl.getText().length() == 0) return false;
		File dir = new File(txtDest.getText());
		if(!(dir.exists() && dir.isDirectory())) return false;
		
		switch(combo.getSelectionIndex()) {
			case 0:
				if(Flickr.isValidURL(txtUrl.getText()))
					return true;
				else return false;
			case 1:
				if(Chan4.isValidURL(txtUrl.getText()))
					return true;
				else return false;
			default:
				return false;
		}
	}

}
